package com.gp;

import java.util.LinkedList;
import java.util.List;

import com.gp.node.INode;
import com.gp.node.IValue;
import com.gp.node.operator.IFather;
import com.utils.CONST;
import com.utils.Random;

// the mutation implementation
public class Mutation {
	
	// replace subtree
	public static void roughMutation(Chrome chrome){
		if(Random.nextDouble(1)< CONST.MUTATION){
			List<IFather> ls = new  LinkedList<IFather>();
			chrome.fillFathers(ls);
			IFather father = ls.get(Random.nextInt(ls.size()));
			Chrome tmp = ChromeFactory.createChrome(2);
			father.setChild(tmp.getRoot());
		}
	}

	// replace node 
	public static void softMutation(Chrome chrome) { 
		List<INode> nodes = new LinkedList<INode>();
		chrome.fillAll(nodes);
		int index = Random.nextInt(nodes.size());
		if (nodes.get(index) instanceof IValue)
			nodes.set(index, CONST.OP_LIST[Random.nextInt(CONST.OP_LIST.length)]);
		else if (nodes.get(index) instanceof IFather)
			nodes.set(index, CONST.VAL_LIST[Random.nextInt(CONST.VAL_LIST.length)]);
	}
}
